public class Solution3 {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null)
            return null;

        ListNode cur = head.next;
        ListNode prev = head;

        while(cur != null){
            if(cur.val == val){
                cur=cur.next;
                prev.next=cur;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val==val) {
            head=head.next;
        }
        return head;
    }
}
